package com.gc.ykqx.service.util;

import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by wsd on 2017/7/6.
 */

public class DateUtil {
    private static Calendar ca = Calendar.getInstance();
    private static SimpleDateFormat sdf;
    private static final String TAG = "DateUtil";

    /*
    获取最新时间
     */
    public static String getNewTime(String type) {
        String result = null;
        String format = null;
        Date date = new Date();
        sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        switch (type) {
            case Constant.Time.TIME_MIN:
                format = sdf.format(date);
                result = format.substring(0, 11) + "000";
                break;
            case Constant.Time.TIME_HOR:
                format = sdf.format(date);
                result = format.substring(0, 10) + "0000";
                break;
            case Constant.Time.TIME_DAY:
                format = sdf.format(date);
                result = format.substring(0, 8) + "000000";
                // result = subTime(format, type);
                break;
        }
        return result;
    }

    public static String addTime(String time, String type) {
        String result = null;
        Date addDate;
        String format;
        Date nowDate;
        String nowTime = null;
        sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date date = sdf.parse(time);
            ca.setTime(date);
            switch (type) {
                case Constant.Time.TIME_MIN:
                    ca.add(Calendar.MINUTE, 10);
                    //改变后的时间
                    addDate = ca.getTime();
                    format = sdf.format(addDate);
                    result = format.substring(0, 11) + "000";
                    //当前时间
                    nowDate = new Date();
                    format = sdf.format(nowDate);
                    nowTime = format.substring(0, 11) + "000";
                    break;
                case Constant.Time.TIME_HOR:
                    ca.add(Calendar.HOUR_OF_DAY, 1);
                    addDate = ca.getTime();
                    format = sdf.format(addDate);
                    result = format.substring(0, 10) + "0000";
                    //当前时间
                    nowDate = new Date();
                    format = sdf.format(nowDate);
                    nowTime = format.substring(0, 10) + "0000";
                    break;
                case Constant.Time.TIME_DAY:
                    ca.add(Calendar.DAY_OF_MONTH, 1);
                    addDate = ca.getTime();
                    format = sdf.format(addDate);
                    result = format.substring(0, 8) + "000000";
                    //当为天时 以昨天为终点
                    nowDate = new Date();
                    ca.setTime(nowDate);
                    //    ca.add(Calendar.DAY_OF_MONTH, -1);
                    Date yesDate = ca.getTime();
                    format = sdf.format(yesDate);
                    nowTime = format.substring(0, 8) + "000000";
                    break;
            }
            if (nowTime != null && result != null && Double.parseDouble(nowTime) >= Double.parseDouble(result)) {
                return result;
            } else {
                return null;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String subTime(String time, String type) {
        String result = null;
        Date addDate;
        String format;
        Date nowDate;
        String nowTime = null;
        sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date date = sdf.parse(time);
            ca.setTime(date);
            switch (type) {
                case Constant.Time.TIME_MIN:
                    ca.add(Calendar.MINUTE, -10);
                    //改变后的时间
                    addDate = ca.getTime();
                    format = sdf.format(addDate);
                    result = format.substring(0, 11) + "000";
                    //当前时间
                    nowDate = new Date();
                    format = sdf.format(nowDate);
                    nowTime = format.substring(0, 11) + "000";
                    break;
                case Constant.Time.TIME_HOR:
                    ca.add(Calendar.HOUR_OF_DAY, -1);
                    addDate = ca.getTime();
                    format = sdf.format(addDate);
                    result = format.substring(0, 10) + "0000";
                    //当前时间
                    nowDate = new Date();
                    format = sdf.format(nowDate);
                    nowTime = format.substring(0, 10) + "0000";
                    break;
                case Constant.Time.TIME_DAY:
                    ca.add(Calendar.DAY_OF_MONTH, -1);
                    addDate = ca.getTime();
                    format = sdf.format(addDate);
                    result = format.substring(0, 8) + "000000";
                    //当前时间
                    nowDate = new Date();
                    format = sdf.format(nowDate);
                    nowTime = format.substring(0, 8) + "000000";
                    break;
            }
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String add8Hour(String time) {
        String result = time;
        sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date date = sdf.parse(time);
            ca.setTime(date);
            ca.add(Calendar.HOUR_OF_DAY, 8);
            Date addDate = ca.getTime();
            result = sdf.format(addDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return time;
        }
        return result;
    }

    public static String getShowTime(String time, String timeType) {
        switch (timeType) {
            case Constant.Time.TIME_MIN:
            case Constant.Diy.MIN:
                return getShowMin(time);
            case Constant.Time.TIME_HOR:
            case Constant.Diy.Hor:
                return getShowHour(time);
            case Constant.Time.TIME_DAY:
            case Constant.Diy.DAY:
                return getShowDay(time);
            default:
                return time;
        }
    }

    /*
    格式化分钟
    */
    private static String getShowMin(String time) {
        String result = null;
        if (time != null) {
            String newTime = time.substring(4, 12);
            result = newTime.substring(0, 2) + "月" +
                    newTime.substring(2, 4) + "日" +
                    newTime.substring(4, 6) + ":" +
                    newTime.substring(6, 8) + "";
        }
        return result;
    }

    /*
    格式化小时
    */
    private static String getShowHour(String time) {
        String result = null;
        if (time != null && time.length() >= 10) {
            String newTime = time.substring(4, 10);
            result = newTime.substring(0, 2) + "月" +
                    newTime.substring(2, 4) + "日" +
                    newTime.substring(4, 6) + "时";
        }
        return result;
    }

    /*
     格式化天
     */
    private static String getShowDay(String time) {
        String result = null;
        if (time != null) {
            String newTime = time.substring(4, 8);
            result = newTime.substring(0, 2) + "月" +
                    newTime.substring(2, 4) + "日";
        }
        return result;
    }

    /*
    todo this method have problem
     */
    public static boolean isToady(String time) {
        try {
            String newTime = getNewTime(Constant.Time.TIME_DAY);
            if (newTime.equals(time)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            Log.e(TAG, "isToady: " + e.getMessage());
            return false;
        }
    }

    public static String formatDay(String time) {
        String result = null;
        try {
            if (!TextUtils.isEmpty(time)) {
                result = time.substring(0, 8) + "000000";
            }
            return result;
        } catch (Exception e) {
            Log.e(TAG, "formatDay: " + e.getMessage());
        } finally {
            return result;
        }
    }

    public static String formatHor(String time) {
        String result = null;
        try {
            if (!TextUtils.isEmpty(time)) {
                result = time.substring(0, 10) + "0000";
            }
            return result;
        } catch (Exception e) {
            Log.e(TAG, "formatHor: " + e.getMessage());
        } finally {
            return result;
        }
    }

    public static String formatMin(String time) {
        String result = null;
        try {
            if (!TextUtils.isEmpty(time)) {
                result = time.substring(0, 11) + "000";
            }
            return result;
        } catch (Exception e) {
            Log.e(TAG, "formatMin: " + e.getMessage());
        } finally {
            return result;
        }
    }


    public static String getFormatTimeByType(String time, String diyType) {
        try {
            switch (diyType) {
                case Constant.Diy.DAY:
                    time = DateUtil.formatDay(time);
                    break;
                case Constant.Diy.Hor:
                    time = DateUtil.formatHor(time);
                    break;
                case Constant.Diy.MIN:
                    time = DateUtil.formatMin(time);
                    break;
            }
        } catch (Exception e) {
            Log.e(TAG, "getFormatTimeByType: " + e.getMessage());
        } finally {
            return time;
        }

    }

    public static String getForeType() {
        Calendar ca = Calendar.getInstance();
        int hour = ca.get(Calendar.HOUR_OF_DAY);
        if (hour > 23 || hour < 8) {
            return Constant.Fore.SHORT_DATE;
        } else {
            return Constant.Fore.SHORT_TIME;
        }
    }
}
